package com.mt.system.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.system.entity.AdminRoleRelateEntity;
import com.mt.system.entity.AdminUserEntity;
import com.mt.system.mapper.AdminRoleRelateEntityMapper;
import com.mt.system.mapper.AdminUserEntityMapper;

@Component
public class AdminUserServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AdminUserServiceModel.class);

    @Autowired
    private AdminUserEntityMapper adminUserEntityMapper;
    @Autowired
    private AdminRoleRelateEntityMapper adminRoleRelateEntityMapper;

    public ServiceResult<List<AdminUserEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<List<AdminUserEntity>> result = new ServiceResult<List<AdminUserEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.adminUserEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<AdminUserEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<AdminUserEntity> result = new ServiceResult<AdminUserEntity>();
        try {
            result.setResult(this.adminUserEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> insert(AdminUserEntity adminUserEntity) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.adminUserEntityMapper.insert(adminUserEntity);
            if (save > 0) {
                id = adminUserEntity.getUserId();
                List<AdminRoleRelateEntity> adminRoleRelateList = adminUserEntity.getAdminRoleList();
                if (adminRoleRelateList != null && adminRoleRelateList.size() > 0) {
                    for (AdminRoleRelateEntity adminRoleRelateEntity : adminRoleRelateList) {
                        adminRoleRelateEntity.setUserId(id);
                    }
                    adminRoleRelateEntityMapper.insertList(adminRoleRelateList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> updateRelate(AdminUserEntity adminUserEntity) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.adminUserEntityMapper.updateByPrimaryKeySelective(adminUserEntity);
            if (save > 0) {
                id = adminUserEntity.getUserId();
                // 编辑操作，先删除原有用户角色关联数据，再创建
                adminRoleRelateEntityMapper.deleteByPrimaryKey(id);
                List<AdminRoleRelateEntity> adminRoleRelateList = adminUserEntity.getAdminRoleList();
                if (adminRoleRelateList != null && adminRoleRelateList.size() > 0) {
                    adminRoleRelateEntityMapper.insertList(adminRoleRelateList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(AdminUserEntity adminUserEntity) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.adminUserEntityMapper.updateByPrimaryKeySelective(adminUserEntity);
            if (save > 0) {
                id = adminUserEntity.getUserId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> deleteByPrimaryKey(Integer userId) {
        Assert.notNull(this.adminUserEntityMapper, "Property 'adminUserEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.adminUserEntityMapper.updateByPrimaryKey(userId);
            if (save > 0) {
                // 删除用户关联角色信息
                adminRoleRelateEntityMapper.deleteByPrimaryKey(userId);
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[AdminUserServiceModel][deleteByPrimaryKey]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}
